package com.lyl.security.module.security.service;

import cn.hutool.core.collection.ListUtil;
import com.lyl.security.module.security.model.LoginUser;
import com.lyl.security.module.security.model.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Mr.Luo
 * @date 2022-09-09 12:13
 * @desc
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    static List<User> userList;

    static {
        userList = ListUtil.toList(
                new User(1, "张三", "$2a$10$D2lZ.7w/Qfk0SbfRMQiLEeuU2.pf9MQEfeMMnnzwfISXH4RoqsfMC"),
                new User(2, "李四", "$2a$10$WnucST8lS64OdiJ0LMMafOCCfq8U71Fvx5XblbOuSc8M484QFmbQi")
        );
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userList.stream()
                .filter(u -> Objects.equals(u.getUsername(), username))
                .findFirst().orElseThrow(() -> new UsernameNotFoundException("找不到用户"));
        return new LoginUser(user);
    }

}
